Page({
    data: {
        header:{},
        userInfo:{},
        obj:{
          type:10,
          sex:0,
          item:'武汉',
          title:'',
          model:'',
          "num": 1,//数量
          "price": null,//价格
          "phone": "",//手机号
          "name": "",
          "gender": 1,
          "province": "",
          "city": "",
          "des": "",//描述

        },
        linshiArr:[],
        userInfo:null,
        changeId:null


    },
    turnCity:function(){
      wx.navigateTo({
        url: '/pages/cityChoose/webView?type=7'
      })
    },
    onLoad: function(options) {
      console.log(options.id)
       var userInfo= wx.getStorageSync('userInfo');
      this.setData({
          userInfo:userInfo,
          changeId:options.id
      })
      this.getSheBei(options.id);
    },
    getSheBei:function (id) {
        var userInfo= wx.getStorageSync('userInfo');
         var _self=this;
         wx.request({
            url:getApp().globalData.baseUrl+'/wx/equipment/detail?id='+id+'&openid='+userInfo.openid,
            method:"GET",
            header: {
                'openid':userInfo.openid, //公用请求头
                'content-type': 'application/json'
            },
            success: function(res) {
            wx.setStorageSync('chooseProvince7',res.data.obj.province);
            wx.setStorageSync('chooseCity7',res.data.obj.city);
             console.log(res.data.obj)
             _self.setData({
                obj:res.data.obj
             })
           
           
         }
       })
    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
    onShow: function() {
       var chooseProvince= wx.getStorageSync('chooseProvince7');
       var chooseCity2=wx.getStorageSync('chooseCity7');
        if (chooseProvince) {
           var json=this.data.obj;
          json.province=wx.getStorageSync('chooseProvince7');
          json.city=wx.getStorageSync('chooseCity7');
          this.setData({
            obj:json
          })
        }


    },
    onHide: function() {
        // 生命周期函数--监听页面隐藏

    },
     onMyEvent:function(province){
      console.log(province.detail)
        var json=this.data.obj;
        json.province=province.detail.split(',')[0];
        json.city=province.detail.split(',')[1];
      this.setData({
         obj:json
      })
      
     
    },
    //输入标题
    textTitle:function (e) {
      console.log(e.detail.value);
      var json=this.data.obj;
      json.title=e.detail.value;
      this.setData({
        obj:json
      })
    },
    //输入型号
    textModel:function (e) {
       var json=this.data.obj;
       json.model=e.detail.value;
       this.setData({
        obj:json
      })
    },
    //输入价格
    textPrice:function (e) {
       var json=this.data.obj;
       json.price=e.detail.value;
       this.setData({
        obj:json
      })
    },
    //输入电话
    textPhone:function (e) {
        var json=this.data.obj;
       json.phone=e.detail.value;
       this.setData({
        obj:json
      })
    },
    //输入姓名
    textName:function (e) {
       var json=this.data.obj;
       json.name=e.detail.value;
       this.setData({
        obj:json
      })
    },
    //输入描述
    textDes:function (e) {
         var json=this.data.obj;
       json.des=e.detail.value;
       this.setData({
        obj:json
      })
    },
    //选中设备类型
    chooseType:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
       var json=this.data.obj;
       json.type=e.target.dataset.code;
        this.setData({
            obj:json
        })
    },
    //选中性别类型
    chooseSex:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
        var json=this.data.obj;
        json.gender=e.target.dataset.code;
        this.setData({
            obj:json
        })
    },
    //减
    subButton:function () {
      var num=this.data.obj.num;
      num--;
      if (num<1) {
        num=1
      }
       var json=this.data.obj;
       json.num=num;
      this.setData({
        obj:json
      })
    },
    //加
    addButton:function () {
        // body...
      var num=this.data.obj.num;
      num++;
      if (num<1) {
        num==1
      }
      var json=this.data.obj;
       json.num=num;
      this.setData({
        obj:json
      })
    },
    sendSheBei:function () {
         if (this.data.obj.title==''||this.data.obj.city==''||this.data.obj.phone==''||this.data.obj.name=='') {
          wx.showToast({
          title: '请填写完整信息',
          icon:'none',
          duration: 2000
        })
        return
        }
       var json=this.data.obj;
         json.status=10;
        this.setData({
          obj:json
        })
        if (this.data.obj.wxEquipmentPic.length>6) {
             wx.showToast({
              title: '最多上传6张图片',
              icon:'none',
              duration: 2000
            })
             var  yarr=this.data.obj.wxEquipmentPic.slice(0,6);
             var json=this.data.obj;
             var arr=json.wxEquipmentPic;
             arr=yarr;
             this.setData({
               obj:json
             })
      
       }
        wx.request({
            url: getApp().globalData.baseUrl + '/wx/equipment/update',
            method:"POST",
            data:this.data.obj,
            header: {
                'openid':this.data.userInfo.openid, //公用请求头
                'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
                wx.navigateTo({
                url: '/pages/mineSheBei/mineSheBei'
              })
                                    
            }
            })

    },chooseImage:function () {
      var _self=this;
      wx.chooseImage({
        success (res) {
          const tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths[0])
          var imgUrl=tempFilePaths[0]
         
          wx.uploadFile({
            url:getApp().globalData.baseUrl+'/wx/img/url', //仅为示例，非真实的接口地址
            //url:'http://172.31.1.79:3000/users/uploadImg',
            filePath: tempFilePaths[0],
            header: {
              'content-type':'multipart/form-data',
              'openid':_self.data.userInfo.openid 
            },
            name: 'filePath',
            formData: {
              openid:_self.data.userInfo.openid 
            },
            success (res){
                 var data = JSON.parse(res.data);
                  var json=_self.data.obj;
                  var arr=json.wxEquipmentPic;
                  var a={url:data.obj}
                  arr.push(a)
                    _self.setData({
                        obj:json
                    })
            }
          })
        }
      })
    },
     deleteImg:function (e) {
       console.log(e.target.dataset.code);
       var index=e.target.dataset.code;
       var json=this.data.obj;
       var arr=json.wxEquipmentPic;
       arr.splice(index,1);
       json.wxEquipmentPic=arr;
       this.setData({
        obj:json
       })
    }

})